package com.click369.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.click369.crm.mapper.CStaffMapper;
import com.click369.crm.pojo.CStaff;
import com.click369.crm.service.StaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class StaffServiceImpl implements StaffService{
	
	@Autowired
	private CStaffMapper staffMapper;
	//添加
	@Override
	public void save(CStaff staff) {
		staff.setCreateTime(new Date());
		staffMapper.insert(staff);
	}
	//根据id删除
	@Override
	public void delete(Integer id) {
		staffMapper.deleteByPrimaryKey(id);
	}
	//修改
	@Override
	public void update(CStaff staff) {
		staff.setUpdateTime(new Date());
		String password = staff.getPassword();
		//密码加密
		password = DigestUtils.md5DigestAsHex(password.getBytes());
		staff.setPassword(password);
		staffMapper.updateByPrimaryKeySelective(staff);
	}
	//根据id查询
	@Override
	public CStaff getById(Integer id) {
		return staffMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public PageInfo findPage(Integer page, Integer rows, CStaff staff) {
		//分页
		PageHelper.startPage(page,rows);
		List list = new ArrayList();
		list=staffMapper.findListWithPname(staff);
		return new PageInfo(list);
	}
	
	@Override
	public PageInfo<CStaff> select(Integer page, Integer rows, CStaff t) {
		return null;
	}
	
	@Override
	public PageInfo<HashMap> selectAdmin(Integer page, Integer rows) {
		//分页
		PageHelper.startPage(page,rows);
		List<HashMap> admins =staffMapper.selectAdmin();
		return new PageInfo(admins);
	}
	
	@Override
	public CStaff getStaffByUsernameAndPassword(String username, String password) {
		//密码加密
		password = DigestUtils.md5DigestAsHex(password.getBytes());
		List<CStaff> staffs = this.staffMapper.getStaffByUsernameAndPassword(username, password);
		//如果有数据
		if(staffs.size()>0){
			return staffs.get(0);
		}
		return null;
	}
	@Override
	public HashSet getRoleCodeByUsername(String username) {
		return staffMapper.getRoleCodeByUsername(username);
	}
	@Override
	public HashSet getFunctionCodeByUsername(String username) {
		return staffMapper.getFunctionCodeByUsername(username);
	}
}
